package cn.com.okgotest.fragment;

import android.Manifest;
import android.os.Bundle;
import android.os.Environment;
import android.support.annotation.NonNull;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;

import com.github.jlmd.animatedcircleloadingview.AnimatedCircleLoadingView;
import com.yanzhenjie.permission.AndPermission;
import com.yanzhenjie.permission.PermissionListener;
import com.yanzhenjie.permission.Rationale;
import com.yanzhenjie.permission.RationaleListener;

import java.io.File;
import java.util.List;

import cn.com.okgo.OkGo;
import cn.com.okgo.callback.HttpFileCallback;
import cn.com.okgotest.R;
import cn.com.okgotest.fragment.base.BaseFragment;

public class DownloadFragment extends BaseFragment {

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.fragment_download, container, false);
        init(view);
        return view;
    }

    private void init(View view) {
        final Button button = view.findViewById(R.id.downloadBtn);
        final AnimatedCircleLoadingView progressBar = view.findViewById(R.id.progressBar);
        final String path = Environment.getExternalStorageDirectory() + File.separator + "okgotest";

        AndPermission.with(this)
                .requestCode(0x1010)
                .permission(Manifest.permission.MOUNT_UNMOUNT_FILESYSTEMS,
                        Manifest.permission.WRITE_EXTERNAL_STORAGE,
                        Manifest.permission.ACCESS_NETWORK_STATE)
                .callback(new PermissionListener() {

                    @Override
                    public void onSucceed(int requestCode, @NonNull List<String> grantPermissions) {
                        for (String s : grantPermissions) {
                            Log.e("OkgoTestApplication", "成功===>>" + s);
                        }
                    }

                    @Override
                    public void onFailed(int requestCode, @NonNull List<String> deniedPermissions) {
                        for (String s : deniedPermissions) {
                            Log.e("OkgoTestApplication", "失败===>>" + s);
                        }
                    }
                })
                .start();

        button.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                button.setText("开始下载");
                button.setEnabled(false);
                startDeterminate(progressBar);
                progressBar.setPercent(0);
                OkGo.download("http://gdown.baidu.com/data/wisegame/0904344dee4a2d92/QQ_718.apk")
                        .execute(new HttpFileCallback(path, "QQ_718.apk") {
                            @Override
                            public void onSuccess(File obj, String baseUrl) {
                                button.setEnabled(true);
                                button.setText("下载成功");
                                progressBar.stopOk();
                            }

                            @Override
                            public void onFailed(Exception e, String baseUrl) {
                                button.setEnabled(true);
                                button.setText("下载失败");
                                progressBar.stopFailure();
                                Log.e("DownloadFragment", e.toString());
                            }

                            @Override
                            public void onDownloadProgress(float progress, long total) {
                                progressBar.setPercent((int) (progress * 100));
                            }
                        });
            }
        });
    }

}
